<template>
    <view id="section_ul" class="selectTrees" style="width: 90%;margin: 0 auto;">
        <!-- 一级分支 -->
        <view class="lv1list" v-for="(item, index) in selectList" :key="index">
            <view class="tree-one">
                <!-- 单选框组件 -->
                <checkbox-group v-if="showCheck"
                    style="position: absolute;height: 80rpx;line-height: 80rpx; left:90rpx;z-index: 1;">
                    <checkbox style="transform:scale(1.1)" :checked="item.checked" @click="_chooseAll(item,index)" />
                </checkbox-group>
                <!-- 名字和iconfont -->
                <label
                    style="height:80rpx;display: flex;align-items: center;padding: 20rpx;position: relative;"
                    @click="_showlv2(index)">
                    <view class="itemT">{{item.name}}</view>
                    <view class="deleteBtn" v-if="showDelete" @click.stop="deleteItem(item)">删除</view>
                    <i class="cuIcon-fold" v-if="item.show"
                        style="position: absolute;top: 35%;right: 2%;font-size: 16px;"></i>
                    <i class="cuIcon-unfold" v-else
                        style="position: absolute;top: 35%;right: 2%;font-size: 16px;"></i>
                </label>
            </view>
            <!-- 二级分支 -->
            <view v-if='item.show && item.childrenList '>
                <view class="tree-two" v-for="(item2, index2) in item.childrenList" :key="index2"
                    style="display: flex;">
                    <view class="aui-list-item-inner flexIn">
                        <checkbox-group v-if="showCheck">
                            <checkbox style="transform:scale(0.9)" v-if="!disableLv2Check" :checked="item2.checked"
                                @click="_chooseOne(index,index2)" />
                            <checkbox style="transform:scale(0.9)" :checked="item2.checked" disabled="true" v-else />
                        </checkbox-group>
                        <label style="font-size: 28rpx;margin-left: 10px;">{{item2.name}}</label>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
export default {
    name: "select-tree",
    data() {
        return {
            finalList: [],
            menuKey: 1,
            selectList: this.value
        };
    },
    props: {
        // selectList,
        value: {
            type: Array,
            default: function () {
                return [
                    {
                        name: "首页",
                        checked: false,
                        show: false,
                        childrenList: [
                            {
                                checked: false,
                                name: "设备统计"
                            },
                            {
                                checked: false,
                                name: "运行时长"
                            },
                            {
                                checked: false,
                                name: "产量分析"
                            },
                            {
                                checked: false,
                                name: "订单进度"
                            },
                            {
                                checked: false,
                                name: "能耗总览"
                            },
                            {
                                checked: false,
                                name: "开机率对比"
                            }
                        ]
                    },
                    {
                        name: "设备",
                        checked: false,
                        show: false,
                        childrenList: [
                            {
                                checked: false,
                                name: "实时状态"
                            },
                            {
                                checked: false,
                                name: "设备明细"
                            },
                            {
                                checked: false,
                                name: "生产周期"
                            },
                            {
                                checked: false,
                                name: "时长分析"
                            },
                            {
                                checked: false,
                                name: "故障分析"
                            },
                            {
                                checked: false,
                                name: "优化建议"
                            },
                            {
                                checked: false,
                                name: "生产日志"
                            },
                            {
                                checked: false,
                                name: "周期对比"
                            }
                        ]
                    },
					{
					    name: "能耗",
					    checked: false,
					    show: false,
					    childrenList: [
					        {
					            checked: false,
					            name: "实时能耗"
					        },
					        {
					            checked: false,
					            name: "能耗分析"
					        },
					        {
					            checked: false,
					            name: "能耗产量对比"
					        },
					        {
					            checked: false,
					            name: "能耗优化建议"
					        }
					    ]
					},
					{
					    name: "统计",
					    checked: false,
					    show: false,
					    childrenList: [
					        {
					            checked: false,
					            name: "产量统计"
					        },
					        {
					            checked: false,
					            name: "产能分析"
					        },
					        {
					            checked: false,
					            name: "能耗统计"
					        },
					        {
					            checked: false,
					            name: "能耗分析"
					        },
					        {
					            checked: false,
					            name: "时长统计"
					        },
					        {
					            checked: false,
					            name: "时长分析"
					        },
					        {
					            checked: false,
					            name: "产能优化建议"
					        },
					        {
					            checked: false,
					            name: "产能对比"
					        },
					        {
					            checked: false,
					            name: "生产率"
					        }
					    ]
					}
                ];
            }
        },
        showCheck: {
            //显示多选框
            type: Boolean,
            default: true
        },
        disableLv2Check: {
            //让二级标题不可选中
            type: Boolean,
            default: false
        },
        showDelete: {
            //显示删除按钮
            type: Boolean,
            default: false
        }
    },
    mounted() {
        // console.log(this.value)
        // setTimeout(()=>{
        //      this.selectList = this.value
        // })

        // this.$forceUpdate()
    },
    methods: {
        _showlv2(index) {
            // console.log('showLv2............................', index)
            //展开二级目录
            if (this.selectList[index].show) {
                this.$set(this.selectList[index], "show", false);
				this.$forceUpdate()
                this.$emit('input', this.selectList)
            } else {
                this.$set(this.selectList[index], "show", true);
				this.$forceUpdate()
                this.$emit('input', this.selectList)
            }
        },
        _chooseAll(item, index) {
            //选中一级目录的所有
            if (this.selectList[index].checked) {
                //
                this.$set(this.selectList[index], "checked", false);
                this.selectList[index].childrenList.forEach(item => {
                    item.checked = false;
                });
                this.$emit('input', this.selectList)
            } else {
                this.$set(this.selectList[index], "checked", true);
                this.selectList[index].childrenList.forEach(item => {
                    item.checked = true;
                });
                this.$emit('input', this.selectList)
            }
            this.$set(this.selectList[index], "show", true);
            this.$emit('input', this.selectList)
            this.$forceUpdate();
            this._computedFinalList();
        },
        _chooseOne(i1, i2) {
            if (this.selectList[i1].childrenList[i2].checked) {
                //去掉勾选
                this.$set(this.selectList[i1], "checked", true);
                this.$set(this.selectList[i1].childrenList[i2], "checked", false);
				if (
				    this.selectList[i1].childrenList.every(item => item.checked == false)
				) {
				    //判断是否全部都是选中
				    this.$set(this.selectList[i1], "checked", false);
				}
                this.$forceUpdate();
                this.$emit('input', this.selectList)
                this._computedFinalList();

            } else {
                //增加勾选
                this.$set(this.selectList[i1], "checked", true);
                this.$set(this.selectList[i1].childrenList[i2], "checked", true);
                if (
                    this.selectList[i1].childrenList.every(item => item.checked == true)
                ) {
                    //判断是否全部都是选中
                    this.$set(this.selectList[i1], "checked", true);
                }
                this.$emit('input', this.selectList)
                this.$forceUpdate();
                this._computedFinalList();
            }
        },
        _computedFinalList() {
            //计算最终的值
            this.finalList = [];
			this.selectList.forEach(item=>{
				if(item.checked){
					this.finalList.push(JSON.parse(JSON.stringify(item))) //对象深拷贝 不然原数据会发生变化
				}
			})
			this.finalList.forEach(item=>{
				item.childrenList.forEach((item2,index2)=>{
					if(!item2.checked){
						item.childrenList.splice(index2,1)
					}
				})
			})
            this.$emit("choose", this.finalList);
        },
        chooseAll() {
            //选中页面所有层级
            this.selectList.forEach(item => {
                this.$set(item, "checked", true);
                item.childrenList.forEach(item2 => {
                    this.$set(item2, "checked", true);
                });
            });
            this.$emit('input', this.selectList)
            this.$forceUpdate();
            this._computedFinalList();
        },
        cancelAll() {
            //取消选中页面所有层级
            this.selectList.forEach(item => {
                this.$set(item, "checked", false);
                item.childrenList.forEach(item2 => {
                    this.$set(item2, "checked", false);
                });
            });
            this.$emit('input', this.selectList)
            this.$forceUpdate();
            this._computedFinalList();
        },
        deleteItem(item) {
            //删除选中项目
            this.$emit('input', this.selectList)
            this.$emit("deleteItem", item);
        }
    },
    watch: {
        value(res) {
            console.log(res)
            this.selectList = res
            this.$emit('input', this.selectList)
        }
    },
};
</script>

<style lang="scss" scoped>
* {
    font-size: 32rpx;
}

/* #ifdef APP-PLUS*/
.selectTrees{
    margin-bottom: 180rpx;
}
/* #endif */

.deleteBtn {
    position: absolute;
    right: 10%;
    background: #f97979;
    padding: 2rpx 16rpx;
    color: white;
    border-radius: 4rpx;
    font-size: 28rpx;
}

.itemT {
    margin-left: 60rpx;
    font-size: 32rpx;
    width: 65%;
    text-overflow: -o-ellipsis-lastline;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
}

.tree-two {
    padding: 20rpx 68rpx;
    // background: white;
    // border-bottom: 2rpx solid #e2e2e2;
    font-size: 28rpx;
}

.flexIn {
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    align-content: center;
    flex-wrap: nowrap;
    width: 100%;
}
</style>